package com.ruoyi.contract.service.impl;


import com.ruoyi.contract.domain.Cproject;
import com.ruoyi.contract.mapper.ContractMapper;
import com.ruoyi.contract.model.Query1;
import com.ruoyi.contract.service.ContractService;
import com.ruoyi.experiment.domain.Experiment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    ContractMapper mapper;


    @Override
    public List<Cproject> list(Query1 query1) {
        List<Cproject> cprojects = mapper.selectAll(query1);
        for (Cproject cproject : cprojects) {
            if (cproject.getCode()!=null && cproject.getHaveContract()==0){
                List<Experiment> experiments = mapper.selectExperimentPrice(cproject.getProjectId());
                long sum=0l;
                for (Experiment experiment : experiments) {
                    sum+= experiment.getExperimentPrice()*experiment.getExperimentNumber()*100;
                }
                cproject.setTotalAmount(sum);
                mapper.updateContractPrice(cproject);
            }
        }
        return cprojects;
    }

    @Override
    public Long add1(Cproject cproject) {
        mapper.insertContract(cproject);
        return cproject.getProjectId();
    }

    @Override
    public Cproject getOne(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public boolean update(Cproject cproject) {
        mapper.updateContract(cproject);
        return true;
    }

    @Override
    @Transactional
    public Integer removeOne(Long id) {
        mapper.removeLCBByPId(id);
        mapper.removeZFByPId(id);
        return mapper.removeOne(id);
    }

    @Override
    public int checkContractCode(String code) {
        return mapper.checkContractCode(code);
    }


}
